package com.movie.demo.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.movie.demo.mapper.MovieMapper;
import com.movie.demo.pojo.Movie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
@Transactional
public class MovieService extends ServiceImpl<MovieMapper, Movie> implements com.movie.demo.service.MovieService {


    @Override
    public List<Movie> selectMovie() {

        List<Movie> movieList=this.selectList(new EntityWrapper<Movie>());
        return movieList;
    }

    @Override
    public Boolean addMovie(Movie movie) {
        Movie movies = this.selectOne(new EntityWrapper<Movie>().eq("Chinese_name", movie.getChinese_name()).or
                ().eq("English_name", movie.getEnglish_name()).or().eq("Img_url",movie.getImg_url()));
        if (Objects.isNull(movies)||movies==null){
            this.insert(movie);
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteMovie(Integer mid) {
        boolean flag = this.deleteById(mid);
        return flag;
    }

    @Override
    public boolean updateMovie(Movie movie) {
        boolean flag = this.updateById(movie);
        return flag;
    }

    @Override
    public List<Movie> searchMovie(String searchstr) {
        List<Movie> movieList=this.selectList(new EntityWrapper<Movie>().like("type",searchstr).or().like("chinese_name",searchstr)
        .or().like("country",searchstr));
        return movieList;
    }
    public List<Movie> searchMovieid(int id) {
        List<Movie> movieList=this.selectList(new EntityWrapper<Movie>().eq("mid",id));
        return movieList;
    }

    @Override
    public List<Movie> topMovie() {
        List<Movie> movieList=this.selectList(new EntityWrapper<Movie>().orderBy("rating",false).last("limit 10"));
        return movieList;
    }
}
